8 Introduction to Spatial Data in R
8.1 Set Up
You will return this assignment similar to last week, by working through this lesson in an R Markdown document, answering the Exercises at the end, and knitting as a Word document to submit to Canvas.
Therefore, begin this lesson by creating a a new R Markdown document, and make sure to select output as Word.
8.1.1 Package Installation
To carry out this lesson, you will need to install a couple new R packages to import and work with spatial data. The two main packages for working with spatial data are sf (for vector data) and terra (for spatial data). We will also be using tmap to visualize spatial data and make quick maps, along with the tigris package to import some vector data.
Run the following chunk of code in your console, comment it out, OR add eval = FALSE in the top of the code chunk. You do not want it to be included when you knit the R Markdown document, because it re-install the packages every time you knit.
install.packages("sf")
install.packages("terra")
install.packages("tmap")
install.packages("tigris")Now we need to read in these packages at the beginning of our workflow. You should have this as an executable code chunk in your R Markdown document.
library(tidyverse)
library(sf)
library(terra)
library(tmap)
library(tigris)8.2 Spatial Data Formats
Vector Data
Locations (points)
- Coordinates, address, country, city
Shapes (lines or polygons)
- Political boundaries, roads, building footprints, water bodies
Raster Data
Images (matrix of cells organized by rows and columns)
Satellite imagery, climate, landcover, elevation

8.3 Import and manipulate spatial data
8.3.1 Vector Data
8.3.1.1 tigris
8.3.1.2 Polygons
All the data we are working with in this lesson is confined to the state of Colorado. Let’s start by pulling in political boundaries for Colorado counties with the tigris package, which returns a shapefile consisting of polygons for each county.
# download county shapefile for the state of Colorado
counties <- counties(state = "CO")The tigris package is one of many data retrieval R packages that uses API calls to pull in data from various online/open databases directly into your R session, without the need to separately download. When you close out your R session, these ‘temp’ files are erased, so it does not use up any of your local storage. At the end of this lesson you will learn how to save shapefiles to your computer if you do in fact want to store and use them in the future (e.g., you manipulated a data set quite a bit and don’t want to re-run the entire process every new R session).
8.3.1.3 Lines
tigris has many other data sets in addition to political boundaries. Today let’s work with another shapefile, importing roads for Larimer county, which returns a polyline dataset for all roads in Larimer County.
roads <- roads(state = "CO", county = "Larimer")8.3.1.4 tmap
Throughout this lesson we will be using the tmap package to produce quick static or interactive maps. It should be included in your setup script, but if not be sure to add it now.
tmap allows for both static (“plot” mode) and interactive (“view” mode) mapping options, which you can set using the function tmap_mode() . For today we will be making quick interactive plots. Once you set the mode with tmap_mode(), every plot call to tmap after that produces a plot in that mode.
tmap_mode("view")Lets view our Colorado counties and Larimer County roads shapefiles. To make a “quick thematic map” in tmap you can use the qtm() function. You can also use tm_shape() plus the type of spatial layer (e.g., tm_polygons()) to add your layers to the map if you want to customize the map a little more. Notice how the two following chunks of code produce the same map, but qtm() is much more concise (but limited on customization abilities).
#Using qtm
qtm(counties)#Using tm_shape
tm_shape(counties)+
tm_polygons()+
tm_shape(roads)+
tm_lines()